package com.xuecheng.ucenter.service;

import com.xuecheng.framework.domain.ucenter.XcCompanyUser;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.XcUser;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import com.xuecheng.framework.exception.CustomExcetionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.ucenter.dao.XcCompanyUserRepository;
import com.xuecheng.ucenter.dao.XcUserMapper;
import com.xuecheng.ucenter.dao.XcUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Service
public class XcUserService {
    @Autowired
    XcCompanyUserRepository xcCompanyUserRepository;
    @Autowired
    XcUserRepository xcUserRepository;
    @Autowired
    XcUserMapper xcUserMapper;

    /**
     * @Description: 根据用户名查询用户是否存在
     * @Param: usernmae
     * @return: XcUserExt
     * @Author: Mr.Yuan
     * @Date:
     */
    public XcUserExt getUser(String username) {
        if(StringUtils.isEmpty(username)){
            throw CustomExcetionCast.getCusException(CommonCode.INVALIDPARAM);
        }
//         查询XcUser
        XcUser xcUser = xcUserRepository.findByUsername(username);
        if(xcUser==null){
            return null;
        }
//        查询XcCompanyUser
        XcCompanyUser xcCompanyUser = xcCompanyUserRepository.findByUserId(xcUser.getId());
        XcUserExt xcUserExt=new XcUserExt();
        BeanUtils.copyProperties(xcUser,xcUserExt);

//        查询权限信息
        List<XcMenu> menuList = xcUserMapper.findByUserId(xcUser.getId());
        if(menuList==null){
            menuList=null;
        }else {
            xcUserExt.setPermissions(menuList);
        }

        if(xcCompanyUser==null){
            return xcUserExt;
        }
        xcUserExt.setCompanyId(xcCompanyUser.getCompanyId());
        return xcUserExt;
    }
}
